package com.starrin.trashtalk;

import com.starrin.trashtalk.dao.MatchDAO;
import com.starrin.trashtalk.dao.MatchDAOImpl;
import com.starrin.trashtalk.dao.PlayerDAO;
import com.starrin.trashtalk.dao.PlayerDAOImpl;
import com.starrin.trashtalk.model.Player;
import com.starrin.trashtalk.util.FacesMessageUtil;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by larsstarrin on 15/10/14.
 */
@ManagedBean
@ViewScoped
public class RegisterResultBean implements Serializable {

    @ManagedProperty(value = "#{loginBean}")
    private LoginBean loginBean;

    private List<Player> selectableAwayPlayers;

    private Long homePlayerId;
    private Long awayPlayerId;
    private String homePlayerName;
    private String awayPlayerName;
    private String winningPlayerId;
    private Date matchDate;

    private SetDTO set1 = new SetDTO();
    private SetDTO set2 = new SetDTO();
    private SetDTO set3 = new SetDTO();

    private PlayerDAO playerDAO = new PlayerDAOImpl();
    private MatchDAO matchDAO = new MatchDAOImpl();

    @PostConstruct
    public void init() {
        //if(loginBean.getLoggedInPlayer() != null) {
            homePlayerId = loginBean.getLoggedInPlayer().getId();
            homePlayerName = loginBean.getLoggedInPlayer().fullName();
            selectableAwayPlayers = playerDAO.findAllPlayers();
            selectableAwayPlayers.remove(loginBean.getLoggedInPlayer());
        //}
    }

    public void save() {
        for(Player awayPlayer: selectableAwayPlayers) {
            if(awayPlayer.fullName().equals(awayPlayerName)) {
                awayPlayerId = awayPlayer.getId();
                break;
            }
        }
        boolean success = matchDAO.registerMatchResult(matchDate, homePlayerId, awayPlayerId, set1, set2, set3);
        if(success) {
            FacesMessageUtil.addInfo("Match saved!");
        }
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    public List<Player> getSelectableAwayPlayers() {
        return selectableAwayPlayers;
    }

    public void setSelectableAwayPlayers(List<Player> selectableAwayPlayers) {
        this.selectableAwayPlayers = selectableAwayPlayers;
    }

    public Long getHomePlayerId() {
        return homePlayerId;
    }

    public void setHomePlayerId(Long homePlayerId) {
        this.homePlayerId = homePlayerId;
    }

    public Long getAwayPlayerId() {
        return awayPlayerId;
    }

    public void setAwayPlayerId(Long awayPlayerId) {
        this.awayPlayerId = awayPlayerId;
    }

    public String getWinningPlayerId() {
        return winningPlayerId;
    }

    public void setWinningPlayerId(String winningPlayerId) {
        this.winningPlayerId = winningPlayerId;
    }

    public SetDTO getSet1() {
        return set1;
    }

    public void setSet1(SetDTO set1) {
        this.set1 = set1;
    }

    public SetDTO getSet2() {
        return set2;
    }

    public void setSet2(SetDTO set2) {
        this.set2 = set2;
    }

    public SetDTO getSet3() {
        return set3;
    }

    public void setSet3(SetDTO set3) {
        this.set3 = set3;
    }

    public Date getMatchDate() {
        return matchDate;
    }

    public void setMatchDate(Date matchDate) {
        this.matchDate = matchDate;
    }

    public String getHomePlayerName() {
        return homePlayerName;
    }

    public void setHomePlayerName(String homePlayerName) {
        this.homePlayerName = homePlayerName;
    }

    public String getAwayPlayerName() {
        return awayPlayerName;
    }

    public void setAwayPlayerName(String awayPlayerName) {
        this.awayPlayerName = awayPlayerName;
    }

    public List<String> selectablePlayerNames (String query) {
        List<String> names = new ArrayList<>();
        for(Player player: selectableAwayPlayers) {
            if(player.fullName().toLowerCase().startsWith(query.toLowerCase())) {
                names.add(player.fullName());
            }
        }
        return names;
    }
}
